import debounce from "lodash/debounce";
import * as Vue from "vue";
//@ts-ignore
import { getappvm } from "./appvm"; //@ts-ignore
import { getactivecondition } from "./getactivecondition";
import { resolvemdpathfromurl } from "./resolvemdpathfromurl";
import { scrolltoelement } from "./scrolltoelement";
export const sidebarscroll = debounce(async function sidebarscroll() {
    const appvm = getappvm();

    let path = resolvemdpathfromurl();
    await Vue.nextTick();

    try {
        const links = Array.from(
            (await appvm.getsidebarele()).querySelectorAll("a")
        ) as HTMLAnchorElement[];
        // console.log(links)
        links.forEach((e: HTMLAnchorElement) => {
            let activecondition: boolean = getactivecondition(e, path);
            if (activecondition) {
                requestAnimationFrame(async () => {
                    await Vue.nextTick();
                    requestAnimationFrame(() => {
                        // console.log(e.offsetTop);

                        // console.log(e);
                        //@ts-ignore
                        requestIdleCallback(() => {
                            scrolltoelement(e);
                        });
                    });
                });
            }
        });
    } catch (e) {
        console.error(e);
    }
});
